$(function(){
	$(".banner").thumbnailImg({
	    large_elem: ".large_box",
	    small_elem: ".small_list",
	    left_btn: ".left_btn",
	    right_btn: ".right_btn"
	});

	
	resertWindow();
    $(window).resize(function() {
        resertWindow();
    });
    $('.info-title').click(function(){
        var display =$(this).find('.show-icon').css('display');
        if(display=='none'){
            $(this).find('.hide-icon').click();
        }
        else{
            $(this).find('.show-icon').click();
        }
    });
	$('.show-icon').click(function(e){
		$(this).parents('.info-title').find('.show-icon').hide();
		$(this).parents('.info-title').find('.hide-icon').show();
		$(this).parents('.info-title').next().slideDown();
        e.stopPropagation();
	});
	$('.hide-icon').click(function(e){
		$(this).parents('.info-title').find('.hide-icon').hide();
		$(this).parents('.info-title').find('.show-icon').show();
		$(this).parents('.info-title').next().slideUp();
        e.stopPropagation();
	});

	$('.collect-button').click(function(){
		if($('#openid').val()==''){
            errorAlert('公众号信息配置不完整，暂时无法收藏');
			return;
		}
		$.ajax({
		    type: "post",
		    url: "Api/Product/collect",
		    data: {
		        product_id:$('#product_id').val(),
		        openid:$('#openid').val()
		    },
		    async: true,
		    dataType: 'json',
		    success: function(data) {
		        if (data.status) {
		            if(data.v){
		            	$('.collect-button i').removeClass('fa-heart').addClass('fa-heart-o');
		            	$('.collect-button div').html('收藏');
		            }
		            else{
		            	$('.collect-button i').removeClass('fa-heart-o').addClass('fa-heart');
		            	$('.collect-button div').html('取消收藏');
		            }
		        } else {

		        }
		    }
		});

	});
	doDefault(doShareFriend);
	$('.share-button').click(function(){
		showShare();
	});
	$('.share-zhezhao').click(function(){
        $('.share-zhezhao').hide();
        $('.share-arrow').hide();
    });

    //点击图片进行预览
    $('img').not(".no-click-img").click(function(){
        var current=$(this).attr('src');
        showImages(current);
    });
})

function resertWindow() {
    $('.share-zhezhao').height(Number($(window).height())).width(Number($(window).width()));
}


//获取微信jssdk凭证
function doDefault(successCallBack,errorCallBack) {
    var successCallBack=successCallBack||false; 
    var errorCallBack=errorCallBack||false; 
    $.ajax({
        type: "post",
        url: "CommonApi/CommonWx/getWxJsSdkConfig",
        data: {
            url: window.location.href,
            park_id:$('#park_id').val()
        },
        async: true,
        dataType: 'json',
        success: function(data) {
            if (data.status) {
                var t = JSON.parse(data.v);
                wx.config({
                    beta: true,
                    debug: false,
                    appId: t.appId,
                    timestamp: t.timestamp,
                    nonceStr: t.nonceStr,
                    signature: t.signature,
                    jsApiList: ['checkJsApi',"onMenuShareTimeline", "onMenuShareAppMessage","previewImage"] // 必填，需要使用的JS接口列表
                });
                if(successCallBack){
                    successCallBack();
                }
            } else {
                errorAlert(data.msg);
                if(errorCallBack){
                    errorCallBack();
                }
            }
        }
    });
}

function showShare(){
    $('.share-zhezhao').show();
    $('.share-arrow').show();
}

var doShareFriend=function(){
    wx.ready(function() {
        wx.checkJsApi({
            jsApiList: ['onMenuShareAppMessage','onMenuShareTimeline'], // 需要检测的JS接口列表，所有JS接口列表见附录2,  
            success: function(res) {
                if (res.checkResult.onMenuShareAppMessage == false) {
                    alert("您的微信版本太低！");
                    return;
                }
            }
        });
        var img_url=getRootPath() +'/'+ $('#share-img').val();
        // alert($('.product-code').html());
        // alert($('.product-series').html());
        wx.onMenuShareAppMessage({
            title: $('.product-code').html(), // 分享标题
            desc: $('.product-series').html(), // 分享描述
            link: window.location.href, // 分享链接
            imgUrl: img_url, // 分享图标
            type: 'link', // 分享类型,music、video或link，不填默认为link
            dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
            success: function () {
                $('.share-zhezhao').hide();
                $('.share-arrow').hide();
            },
            cancel: function () {
                $('.share-zhezhao').hide();
                $('.share-arrow').hide();
            }
        });
        wx.onMenuShareTimeline({
            title: $('.product-code').html(), // 分享标题
            desc: $('.product-series').html(), // 分享描述
            link: window.location.href, // 分享链接
            imgUrl: img_url, // 分享图标
            success: function () {
                $('.share-zhezhao').hide();
                $('.share-arrow').hide();
            },
            cancel: function () {
                $('.share-zhezhao').hide();
                $('.share-arrow').hide();
            }
        });
    });
}

var showImages=function(current){
    // alert(document.domain);
    if(current.substr(0,7).toLowerCase() == "http://"||current.substr(0,8).toLowerCase() == "https://"){
        var current=current;
    }
    else{
        if(current.substr(0,8).toLowerCase()=='/ueditor'){
            var current='http://'+document.domain+current;
        }
        else if(current.substr(0,1).toLowerCase()=='/'){
            var current=getRootPath()+current;
        }
        else{
            var current=getRootPath()+'/'+current;
        }  
    }
    wx.ready(function() {
        wx.checkJsApi({
            jsApiList: ['previewImage'], // 需要检测的JS接口列表，所有JS接口列表见附录2,  
            success: function(res) {
                if (res.checkResult.onMenuShareAppMessage == false) {
                    alert("您的微信版本太低！");
                    return;
                }
            }
        });
        var img=[];
        $('img').not(".no-show-img").each(function(){
            if($(this).attr('src').substr(0,7).toLowerCase() == "http://"||$(this).attr('src').substr(0,8).toLowerCase() == "https://"){
                img.push($(this).attr('src'));
            }
            else{
                if($(this).attr('src').substr(0,8).toLowerCase()=='/ueditor'){
                    img.push('http://'+document.domain+$(this).attr('src'));
                }
                else if($(this).attr('src').substr(0,1).toLowerCase()=='/'){
                    img.push(getRootPath()+$(this).attr('src'));
                }
                else{
                    img.push(getRootPath()+'/'+$(this).attr('src'));
                }    
            } 
        });
        wx.previewImage({
            current: current, // 当前显示图片的http链接
            urls: img // 需要预览的图片http链接列表
        });
    });
}